<?php

namespace app\common\model;

use think\Model;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 数据层模型
 */
class Stat extends Model
{

    public $page_info;

    /**
     * 查询新增会员统计
     * @access public
     * @author csdeshang
     * @param array $condition 条件
     * @param string $field 字段
     * @return array
     */
    public function statByMember($where, $field = '*', $page = 0, $order = '', $group = '')
    {
        if ($page) {
        $res = db('member')->field($field)->where($where)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
        } else {
            return db('member')->field($field)->where($where)->page($page)->order($order)->group($group)->select();
        }
        
    }

    /**
     * 查询单条会员统计
     * @access public
     * @author csdeshang
     * @param array $where 条件
     * @param string $field 字段
     * @param string $order 排序
     * @param string $group 分组
     * @return array
     */
    public function getOneByMember($where, $field = '*', $order = '', $group = '')
    {
        return db('member')->field($field)->where($where)->order($order)->group($group)->find();
    }

    /**
     * 查询单条店铺统计
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function getOneByStore($where, $field = '*', $order = '', $group = '')
    {
        return db('store')->field($field)->where($where)->order($order)->group($group)->find();
    }

    /**
     * 查询店铺统计
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function statByStore($where, $field = '*', $order = '', $group = '')
    {
        return db('store')->field($field)->where($where)->group($group)->order($order)->select();
    }

    /**
     * 查询新增店铺统计
     * @access public
     * @author csdeshang
     * @param array $condition 条件
     * @param string $field 字段
     * @param int $page 分页
     * @param string $order 排序
     * @param int $limit 限制
     * @param sting $group 分组
     * @return array
     */
    public function getNewStoreStatList($condition, $field = '*', $page = 0, $order = 'store_id desc', $limit = 0, $group = '') {
        if ($page) {
            $res = db('store')->field($field)->where($condition)->group($group)->order($order)->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $res;
            return $res->items();
        } else {
            return db('store')->field($field)->where($condition)->group($group)->order($order)->limit($limit)->select();
        }
    }

    /**
     * 查询店铺列表
     * @access public
     * @author csdeshang
     * @param array $condition 查询条件
     * @param int $page 分页数
     * @param string $order 排序
     * @param string $field 字段
     * @param string $limit 限制条数
     * @return array
     */
    public function getTongjiStoreList($condition, $page = null, $order = '', $joinin_condition = [],$field = 's.*,w.storeclass_name,j.legal_person', $limit = '') {

        if ($page) {
             $result = db('store')->alias('s')
                     ->join('wholesaleclass w','w.storeclass_id=s.storeclass_id')
                     ->join('Storejoinin j','j.member_id=s.member_id')
               ->field($field)->where($condition)->order($order)->paginate($page,false,['query' => request()->param()]);
            $this->page_info = $result;
            return $result->items();
        } else {
            $result = db('store')->alias('s')
                     ->join('wholesaleclass w','w.storeclass_id=s.storeclass_id')
                     ->join('Storejoinin j','j.member_id=s.member_id')
                     ->field($field)->where($condition)->order($order)->paginate($page,false,['query' => request()->param()]);
            $this->page_info = $result;
            return $result->items();
        }
    }

    public function getYunTongjiStoreList($condition, $page = null, $order = '', $joinin_condition = [], $field = '*', $limit = '') {
        if ($page) {
            $result = $this->field($field)->where($condition)->order($order)->paginate($page,false,['query' => request()->param()]);
            $this->page_info = $result;
            return $result->items();
        } else {
            $result = db('store')->alias('s')
                     ->join('storeclass w','w.storeclass_id=s.storeclass_id')
                     ->join('Storejoinin j','j.member_id=s.member_id')
                     ->field($field)->where($condition)->order($order)->paginate($page,false,['query' => request()->param()]);
            $this->page_info = $result;
            return $result->items();
        }
    }

    /**
     * 查询会员列表
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page 分页
     * @param type $order 排序
     * @return type
     */
    public function getMemberList($where, $field = '*', $page = 0, $order = 'member_id desc')
    {
        $res = db('member')->field($field)->where($where)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }
     /**
     * 查询供应商店铺列表
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page 分页
     * @param type $order 排序
     * @return type
     */
    public function getSupplyList($where, $field = '*', $page = 0, $order = 'supplier_id desc')
    {
        $res = db('supplierstore')->alias('s')
                     ->join('order o','o.order_id=s.order_id')
             ->field($field)->where($where)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }
         /**
     * 查询运营商店铺列表
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page 分页
     * @param type $order 排序
     * @return type
     */
    public function getStatOrderList($where, $field = '*', $page = 0, $order = 'statstore_id desc')
    {
        $res = db('statstore')->alias('s')
                     ->join('order o','o.order_id=s.order_id')
             ->field($field)->where($where)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }

    

    /**
     * 调取店铺等级信息
     * @access public
     * @author csdeshang
     * @return type
     */
    public function getStoreDegree()
    {
        $tmp = db('storegrade')->field('storegrade_id,storegrade_name')->where(true)->select();
        $sd_list = array();
        if (!empty($tmp)) {
            foreach ($tmp as $k => $v) {
                $sd_list[$v['storegrade_id']] = $v['storegrade_name'];
            }
        }
        return $sd_list;
    }

  
    /**
     * 查询会员统计数据记录
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page 分页
     * @param type $limit 限制
     * @param type $order 排序
     * @param type $group 分组
     * @return array
     */
    public function statByStatmember($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
        $res = db('statmember')->field($field)->where($where)->limit($limit)->order($order)->group($group)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }
   #供应商
   public function statBySuppliermember($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
        $res = db('supplierstore')->field($field)->where($where)->limit($limit)->order($order)->group($group)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }
    
    #运营商
       public function statByStatrmember($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
        $res = db('statstore')->field($field)->where($where)->limit($limit)->order($order)->group($group)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }

    #供应商商品件数
   public function statBySupplierNum($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
        $res = db('goodscommon')->field($field)->where($where)->limit($limit)->order($order)->group($group)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }

    /**
     * 查询商品数量
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @return type
     */
    public function getGoodsNum($where)
    {
        $rs = db('goodscommon')->field('count(*) as allnum')->where($where)->select();
        return $rs[0]['allnum'];
    }

    /**
     * 获取预存款数据
     * @access public
     * @author csdeshang 
     * @param type $condition 条件
     * @param type $field 字段
     * @param type $page  分页
     * @param type $order 排序
     * @param type $limit 限制
     * @param type $group 分组
     * @return type
     */
    public function getPredepositInfo($condition, $field = '*', $page = 0, $order = 'lg_addtime desc', $limit = 0, $group = '')
    {
        if ($page) {
        $res = db('pdlog')->field($field)->where($condition)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
        } else {
            return db('pdlog')->field($field)->where($condition)->group($group)->order($order)->limit($limit)->select();
        }
    }

    /**
     * 获取结算数据
     * @access public
     * @author csdeshang
     * @param type $condition 条件
     * @param type $type 类型
     * @param type $have_page 判断分页
     * @return type
     */
    public function getBillList($condition, $type, $have_page = true)
    {
        switch ($type) {
            case 'os'://平台
                return db('orderstatis')->field('sum(os_order_totals) as oot,sum(os_order_returntotals) as oort,sum(os_commis_totals-os_commis_returntotals) as oct,sum(os_store_costtotals) as osct,sum(os_result_totals) as ort')->where($condition)->select();
                break;
            case 'ob'://店铺
                $page = $have_page ? 15 : '';
                $result = db('orderbill')->alias('order_bill')->join('__STORE__ store', 'order_bill.ob_store_id=store.store_id', 'left')->field('order_bill.*,store.member_name')->where($condition)->order('ob_no desc')->paginate($page,false,['query' => request()->param()]);
                $this->page_info = $result;
                return $result->items();
                break;
        }
    }

    /**
     * 获取供应商结算数据
     * @access public
     * @author csdeshang
     * @param type $condition 条件
     * @param type $type 类型
     * @param type $have_page 判断分页
     * @return type
     */
    public function getSupplierList($condition, $type, $have_page = true)
    {
        switch ($type) {
            case 'os'://累计
                return db('supplierstore')->field('sum(supplier_total) as oot')->where($condition)->select();
                break;
            case 'ob'://店铺
                $page = $have_page ? 15 : '';
                $result = db('orderbill')->alias('order_bill')->join('__STORE__ store', 'order_bill.ob_store_id=store.store_id', 'left')->field('order_bill.*,store.member_name')->where($condition)->order('ob_no desc')->paginate($page,false,['query' => request()->param()]);
                $this->page_info = $result;
                return $result->items();
                break;
        }
    }

     /**
     * 获取运营商结算数据
     * @access public
     * @author csdeshang
     * @param type $condition 条件
     * @param type $type 类型
     * @param type $have_page 判断分页
     * @return type
     */
    public function getStatList($condition, $type, $have_page = true)
    {
        switch ($type) {
            case 'os'://累计
                return db('statstore')->field('sum(statstore_total) as oot')->where($condition)->select();
                break;
            case 'ob'://店铺
                $page = $have_page ? 15 : '';
                $result = db('orderbill')->alias('order_bill')->join('__STORE__ store', 'order_bill.ob_store_id=store.store_id', 'left')->field('order_bill.*,store.member_name')->where($condition)->order('ob_no desc')->paginate($page,false,['query' => request()->param()]);
                $this->page_info = $result;
                return $result->items();
                break;
        }
    }

    #会员下单生成（会员下单量，下单件，下单金额）统计数据
    public function generateMember($statm_data){
        db('statmember')->insert($statm_data);
    }

    #会员下单生成商城（收入金额）统计数据
    public function generateMall($statis_data){
        $now_year = date('Y', time());
        $now_month = date('m', time());
        if ($now_month == 1) {
            $search_year = $now_year - 1;
            $search_month = 12;
        } else {
            $search_year = $now_year;

            if ($now_month > 10) {
                $search_month = $now_month - 1;
            } else {
                // $search_month = '0' . ($now_month - 1);
                $search_month = $now_month;
            }
        }
        $year = intval($search_year);
        $month = trim($search_month);

        $statis_data['os_month'] = $year . $month;

        //首先去查数据库中有没有本月的数据
        $statis_info = db('orderstatis')->where(['os_month'=>$statis_data['os_month']])->find();

        if(empty($statis_info)){
            #没有的话添加一条数据
            db('orderstatis')->insert($statis_data);
        }
        #有本月的数据直接更新即可
        $new_statis_data = [
            'os_order_totals' => $statis_data['os_order_totals'] + $statis_info['os_order_totals'], #订单金额
            'os_result_totals' => $statis_data['os_result_totals'] + $statis_info['os_result_totals'], #订单金额
            'os_createdate' => time()
        ];
        db('orderstatis')->where(['os_month'=>$statis_data['os_month']])->update($new_statis_data);
        
    }

    #会员退款生成商城（退款金额）统计数据
    public function generateReturnMall($statis_data){
        $now_year = date('Y', time());
        $now_month = date('m', time());
        if ($now_month == 1) {
            $search_year = $now_year - 1;
            $search_month = 12;
        } else {
            $search_year = $now_year;
            if ($now_month > 10) {
                $search_month = $now_month - 1;
            } else {
                // $search_month = '0' . ($now_month - 1);
                $search_month = $now_month;
            }
        }
        $year = intval($search_year);
        $month = trim($search_month);

        $statis_data['os_month'] = $year . $month;

        //首先去查数据库中有没有本月的数据
        $statis_info = db('orderstatis')->where(['os_month'=>$statis_data['os_month']])->find();

        if(empty($statis_info)){
            #没有的话添加一条数据
            db('orderstatis')->insert($statis_data);
        }
        #有本月的数据直接更新即可
        $new_statis_data = [
            'os_order_returntotals' => $statis_data['os_order_returntotals'] + $statis_info['os_order_returntotals'], #退款金额
            'os_createdate' => time()
        ];
        db('orderstatis')->where(['os_month'=>$statis_data['os_month']])->update($new_statis_data);
        
    }

    #会员生成每个店铺的下单金额统计
    public function generateStoreMall($store_data){
        //首先去查数据库中有没有当前店铺的数据
        $statis_info = db('orderbill')->where(['ob_store_id'=>$store_data['ob_store_id']])->find();

        if(empty($statis_info)){
            #没有的话添加一条数据
            db('orderbill')->insert($store_data);
        }
        #有本店铺的数据直接更新即可
        $new_store_data = [
            'ob_order_totals' => $store_data['ob_order_totals'] + $statis_info['ob_order_totals'], #下单金额
        ];
        db('orderbill')->where(['ob_store_id'=>$store_data['ob_store_id']])->update($new_store_data);
    }
    #会员生成每个店铺的退款金额统计
    public function generateReturnStoreMall($store_data){
        //首先去查数据库中有没有当前店铺的数据
        $statis_info = db('orderbill')->where(['ob_store_id'=>$store_data['ob_store_id']])->find();

        if(empty($statis_info)){
            #没有的话添加一条数据
            db('orderbill')->insert($store_data);
        }
        #有本店铺的数据直接更新即可
        $new_store_data = [
            'ob_order_return_totals' => $store_data['ob_order_return_totals'] + $statis_info['ob_order_return_totals'], #下单金额
        ];
        db('orderbill')->where(['ob_store_id'=>$store_data['ob_store_id']])->update($new_store_data);
    }

    #用户下单供应商金额数据
    public function generateSupplie($supplie_data){
        db('supplierstore')->insert($supplie_data);
    }

    #用户下单运营商金额数据
    public function generateStat($stat_data){
        db('statstore')->insert($stat_data);
    }


    /**
     * 查询订单及订单商品的统计
     * @access public
     * @author csdeshang 
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page 分页
     * @param type $limit 限制
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function statByOrderGoods($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
        $res = db('ordergoods')->alias('ordergoods')->field($field)->join('__ORDER__ order', 'ordergoods.order_id=order.order_id', 'left')->where($where)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }

    /**
     * 查询订单及订单商品的统计
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page  分页
     * @param type $limit 限制
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function statByOrderLog($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
        $res = db('orderlog')->alias('orderlog')->field($field)->join('__ORDER__ order', 'orderlog.order_id = order.order_id', 'left')->where($where)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }

    /**
     * 查询退款退货统计
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page  分页
     * @param type $limit 限制
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function statByRefundreturn($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
        if ($page) {
        $res = db('refundreturn')->field($field)->where($where)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
        } else {
            return db('refundreturn')->field($field)->where($where)->group($group)->order($order)->limit($limit)->select();
        }
    }

    /**
     * 查询店铺动态评分统计
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page  分页
     * @param type $limit 限制
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function statByStoreAndEvaluatestore($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
        $res = db('evaluatestore')->alias('evaluatestore')->field($field)->join('__STORE__ store', 'evaluatestore.seval_storeid=store.store_id', 'left')->where($where)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }

    /**
     * 处理搜索时间
     * @access public
     * @author csdeshang
     * @param type $search_arr 搜索数组
     * @return type
     */
    public function dealwithSearchTime($search_arr)
    {
        //初始化时间
        //天
        if (!isset($search_arr['search_time'])) {
            // $search_arr['search_time'] = date('Y-m-d', time() - 86400);
            $search_arr['search_time'] = date('Y-m-d', time());
        }

        $search_arr['day']['search_time'] = strtotime($search_arr['search_time']); //搜索的时间
        //周
        if (!isset($search_arr['searchweek_year'])) {
            $search_arr['searchweek_year'] = date('Y', time());
        }
        if (!isset($search_arr['searchweek_month'])) {
            $search_arr['searchweek_month'] = date('m', time());
        }
        if (!isset($search_arr['searchweek_week'])) {
            $search_arr['searchweek_week'] = implode('|', getWeek_SdateAndEdate(time()));
        }
        $weekcurrent_year = $search_arr['searchweek_year'];
        $weekcurrent_month = $search_arr['searchweek_month'];
        $weekcurrent_week = $search_arr['searchweek_week'];
        $search_arr['week']['current_year'] = $weekcurrent_year;
        $search_arr['week']['current_month'] = $weekcurrent_month;
        $search_arr['week']['current_week'] = $weekcurrent_week;

        //月
        if (!isset($search_arr['searchmonth_year'])) {
            $search_arr['searchmonth_year'] = date('Y', time());
        }
        if (!isset($search_arr['searchmonth_month'])) {
            $search_arr['searchmonth_month'] = date('m', time());
        }
        $monthcurrent_year = $search_arr['searchmonth_year'];
        $monthcurrent_month = $search_arr['searchmonth_month'];
        $search_arr['month']['current_year'] = $monthcurrent_year;
        $search_arr['month']['current_month'] = $monthcurrent_month;
        return $search_arr;
    }

    /**
     * 获得查询的开始和结束时间
     * @access public
     * @author csdeshang
     * @param type $search_arr 搜索数组
     * @return type
     */
    public function getStarttimeAndEndtime($search_arr)
    {
        $stime=array();
        $etime=array();
        if (isset($search_arr['search_type'])&&$search_arr['search_type'] == 'day') {
            $stime = $search_arr['day']['search_time']; //今天0点
            $etime = $search_arr['day']['search_time'] + 86400 - 1; //今天24点
        }
        if (isset($search_arr['search_type'])&&$search_arr['search_type'] == 'week') {
            $current_weekarr = explode('|', $search_arr['week']['current_week']);
            $stime = strtotime($current_weekarr[0]);
            $etime = strtotime($current_weekarr[1]) + 86400 - 1;
        }
        if (isset($search_arr['search_type'])&&$search_arr['search_type'] == 'month') {
            $stime = strtotime($search_arr['month']['current_year'] . '-' . $search_arr['month']['current_month'] . "-01 0 month");
            $etime = getMonthLastDay($search_arr['month']['current_year'], $search_arr['month']['current_month']) + 86400 - 1;
        }
        return array($stime, $etime);
    }

    /**
     * 查询会员统计数据单条记录
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function getOneStatmember($where, $field = '*', $order = '', $group = '')
    {
        return db('statmember')->field($field)->where($where)->group($group)->order($order)->find();
    }

    /**
     * 更新会员统计数据单条记录
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $update_arr 更新数据
     * @return type
     */
    public function editStatmember($where, $update_arr)
    {
        return db('statmember')->where($where)->update($update_arr);
    }

    /**
     * 查询订单的统计
     * @access public
     * @author csdeshang
     * @param array $where 条件
     * @param string $field 字段
     * @param int $page 分页
     * @param int $limit 限制
     * @param string $order 排序
     * @return array
     */
    public function statByOrder($where, $field = '*', $page = 0, $limit = 0, $order = '')
    {
        $res = db('order')->field($field)->where($where)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }

    /**
     * 查询积分的统计
     * @access public
     * @author csdeshang
     * @param array $where 条件
     * @param string $field 字段
     * @param int $page 分页
     * @param int $limit 限制
     * @param string $order 排序
     * @param string $group 分组
     */
    public function statByPointslog($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {

        $res = db('pointslog')->field($field)->where($where)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }

    /**
     * 删除会员统计数据记录
     * @access public
     * @author csdeshang
     * @param type $where 条件数组
     */
    public function delByStatmember($where = array())
    {
        db('statmember')->where($where)->delete();
    }

    /**
     * 查询订单商品缓存的统计
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function getoneByStatordergoods($where, $field = '*', $order = '', $group = '')
    {
        return db('statordergoods')->field($field)->where($where)->group($group)->order($order)->find();
    }

    /**
     * 查询订单商品缓存的统计
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page 分页
     * @param type $limit 限制
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function statByStatordergoods($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
        
        if ($page) {
        $res = db('statordergoods')->field($field)->where($where)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
        } else {
            return db('statordergoods')->field($field)->where($where)->group($group)->order($order)->limit($limit)->select();
        }
    }

    /**
     * 查询订单缓存的统计
     * @access public
     * @author csdeshang
     * @param array $where 条件
     * @param string $field 字段
     * @param string $order 排序
     * @param string $group 分组
     * @return array
     */
    public function getoneByStatorder($where, $field = '*', $order = '', $group = '')
    {
        return db('statorder')->field($field)->where($where)->group($group)->order($order)->find();
    }

    /**
     * 查询订单缓存的统计
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page  分页
     * @param type $limit 限制
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function statByStatorder($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '') {
        if ($page) {
            $res = db('statorder')->field($field)->where($where)->group($group)->order($order)->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $res;
            return $res->items();
        } else {
            return db('statorder')->field($field)->where($where)->group($group)->order($order)->limit($limit)->select();
        }
    }

    /**
     * 查询商品列表
     * @access public
     * @author csdeshang
     * @param type $where 条件
     * @param type $field 字段
     * @param type $page 分页
     * @param type $limit 限制
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function statByGoods($where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {

        $res = db('goods')->field($field)->where($where)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info = $res;
        return $res->items();
    }

    /**
     * 查询流量统计单条记录
     * @access public
     * @author csdeshang
     * @param type $tablename 表名
     * @param type $where 条件
     * @param type $field 字段
     * @param type $order 排序
     * @param type $group 分组
     * @return type
     */
    public function getoneByFlowstat($tablename = 'flowstat', $where, $field = '*', $order = '', $group = '')
    {
        return db($tablename)->field($field)->where($where)->group($group)->order($order)->find();
    }

    /**
     * 查询流量统计记录
     * @access public
     * @author csdeshang
     * @param string $tablename 表名
     * @param array $where 条件
     * @param string $field 字段
     * @param int $page 分页
     * @param int $limit 限制
     * @param string $order 排序
     * @param string $group 分组
     * @return array
     */
    public function statByFlowstat($tablename = 'flowstat', $where, $field = '*', $page = 0, $limit = 0, $order = '', $group = '')
    {
		if ($page) {
			$res = db($tablename)->field($field)->where($where)->group($group)->order($order)->paginate($page,false,['query' => request()->param()]);
			$this->page_info = $res;
			return $res->items();
        } else {
            return db($tablename)->field($field)->where($where)->group($group)->order($order)->limit($limit)->select();
        }
    }

}
